<?php
$modif_cart = "UPDATE web_cart SET date_modified = now() WHERE id = ".$_SESSION["cart.id"];
switch($submenu)
{
	default:
	die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
	break;
	
	case "cart_add":
	if(!empty($_GET["id"]))
	{
		$cart_id = $_SESSION["cart.id"];
		$customer_id = $_SESSION["login.id"];
		$good_id = $_GET["id"];
		if(empty($cart_id))
		{
			$msg1 ="Tambah cart belum berhasil";
			$msg2 ="<br /> Silahkan mencoba kembali setelah beberapa saat. 
					<br /> Jika Anda masih menemui pesan error ini, segera hubungi <b>Admnistrator Website</b>.";
			$msg3 ="<br />Segera hubungi <b>Administrator Website</b> untuk memastikan tidak terjadi redudansi data order.";
			$turn_off = mysql_query("SET AUTOCOMMIT = OFF");
			if(!$turn_off)
			{
				$_SESSION["barang.warning_box"] = "warning";
				$_SESSION["barang.warning_msg"] = "$msg1 - setting <b>autocommit</b> bermasalah. $msg2";
				die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
			}
			$start = mysql_query("START TRANSACTION");
			if(!$start)
			{
				$_SESSION["barang.warning_box"] = "warning";
				$_SESSION["barang.warning_msg"] = "$msg1 - setting <b>transactions<b> bermasalah. $msg2";
				die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
			}
			$insert_header = mysql_query("INSERT INTO web_cart (customer_id,date_created) VALUES ($customer_id,now())");
			if(!$insert_header)
			{
				$_SESSION["barang.warning_box"] = "warning";
				$_SESSION["barang.warning_msg"] = "$msg1 - data header tidak dapat disimpan";
				die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
			}
			$new_cart_id = mysql_insert_id();
			$insert_detail = mysql_query("INSERT INTO web_cart_detail (cart_id,good_id,date_created) VALUES ($new_cart_id,$good_id,now())");
			if(!$insert_detail)
			{
				$_SESSION["barang.warning_box"] = "warning";
				$_SESSION["barang.warning_msg"] = "$msg1 - data detail tidak dapat disimpan";
				$rollback = mysql_query("ROLLBACK");
				if(!$rollback)
				{
					$_SESSION["barang.warning_box"] = "error";
					$_SESSION["barang.warning_msg"] .= "<br />Rollback transaction gagal. $msg3";
				}
				die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
			}
			$new_cart_detail_id = mysql_insert_id();
			$commit = mysql_query("COMMIT");
			if(!$commit)
			{
				$_SESSION["barang.warning_box"] = "error";
				$_SESSION["barang.warning_msg"] = "$msg1 - commit transaction gagal. $msg2";
				die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
			}
			$_SESSION["cart.id"] = $new_cart_id;
			$_SESSION["$menu.warning_box"] = "valid";
			$_SESSION["$menu.warning_msg"] = "Barang telah ditambahkan ke dalam cart, silahkan menentukan detail barang";
			die("<meta content='0;URL=?m=$menu&md=cart_edit&id=".$new_cart_detail_id."' http-equiv='refresh' />");
		}
		else
		{
			$get_detail = mysql_query("SELECT * FROM web_cart_detail WHERE cart_id = $cart_id AND good_id = $good_id AND status = 1 ORDER BY date_modified DESC, date_created DESC");
			$check_detail = mysql_num_rows($get_detail);
			if($check_detail > 0)
			{
				$detail = mysql_fetch_array($get_detail);
				$_SESSION["$menu.warning_box"] = "warning";
				$_SESSION["$menu.warning_msg"] = "Barang sudah diorder di dalam cart";
				die("<meta content='0;URL=?m=$menu&md=cart_edit&id=".$detail["id"]."' http-equiv='refresh' />");
			}
			else
			{
				$insert_detail = mysql_query("INSERT INTO web_cart_detail (cart_id,good_id,date_created) VALUES ($cart_id,$good_id,now())");
				if($insert_detail)
				{
					$new_cart_detail_id = mysql_insert_id();
					mysql_query($modif_cart);
					$_SESSION["$menu.warning_box"] = "valid";
					$_SESSION["$menu.warning_msg"] = "Barang telah ditambahkan ke dalam cart, silahkan menentukan detail barang";
					die("<meta content='0;URL=?m=$menu&md=cart_edit&id=".$new_cart_detail_id."' http-equiv='refresh' />");
				}
				else
				{
					$_SESSION["cart.warning_box"] = "error";
					$_SESSION["cart.warning_msg"] = "Barang gagal ditambahkan ke dalam cart - detail gagal disimpan";
				}
			}
		}
	}
	die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
	break;
	
	case "cart_edit":
	$r = mysql_fetch_array(mysql_query("
	SELECT d.id, d.unit1_id AS SatuanHarga, d.unit2_id AS SatuanUnit, b.intNomor AS NomorBarang, b.vcKode AS KodeBarang, b.vcNamaBeli AS NamaBarang, 
	kat.vcNama AS NamaKategori, b.intNomorMBrand AS NomorBrand, brand.vcNama AS NamaBrand, grup.vcNama AS NamaGroup, 
	tipe.vcNama AS NamaTipe, grade.vcNama AS NamaGrade, sur.vcNama AS NamaSurface
	FROM web_cart_detail d JOIN mbarang b ON d.good_id = b.intNomor
	JOIN mkategori kat ON b.intNomorMKategori = kat.intNomor JOIN mbrand brand ON b.intNomorMBrand = brand.intNomor
	JOIN mgroup grup ON b.intNomorMGroup = grup.intNomor JOIN mtipe tipe ON b.intNomorMTipe = tipe.intNomor
	JOIN mgrade grade ON b.intNomorMGrade = grade.intNomor JOIN msurface sur ON b.intNomorMSurface = sur.intNomor
	WHERE d.id = ".$_GET["id"]));
	$valuta = $_SESSION["setting.valuta"];
	$nomor_barang = $r["NomorBarang"];
	$get_satuan_harga = mysql_query("
	SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, intNomorMSatuan1 AS NomorSatuan1, decKonversi AS NilaiKonversi, decHarga AS PriceList
	FROM (
		SELECT 1 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, 1 AS decKonversi,
			CASE
				WHEN $valuta = 1 THEN a.decHargaPL1
				WHEN $valuta = 2 THEN a.decHargaPL1USD
				WHEN $valuta = 3 THEN a.decHargaPL1RMB
			END AS decHarga
		FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
		UNION ALL
		SELECT 2 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 AS decKonversi,
			CASE
				WHEN $valuta = 1 THEN a.decHargaPL2
				WHEN $valuta = 2 THEN a.decHargaPL2USD
				WHEN $valuta = 3 THEN a.decHargaPL2RMB
			END AS decHarga
		FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
		UNION ALL
		SELECT 3 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 * decKonversi3 AS decKonversi,
			CASE 
				WHEN $valuta = 1 THEN a.decHargaPL3
				WHEN $valuta = 2 THEN a.decHargaPL3USD
				WHEN $valuta = 3 THEN a.decHargaPL3RMB
			END AS decHarga
		FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
		WHERE a.intNomor = $nomor_barang AND b.intNomor > 0)
	a ORDER BY a.intOrder");
	$html_satuan_harga = "<option value=\"\">--pilih satuan harga--</option>";
	$html_hidden_harga = "";
	while($satuan = mysql_fetch_array($get_satuan_harga))
	{
		if($satuan["PriceList"] > 0)
		{
			$html_satuan_harga .= "<option value=\"".$satuan["NomorSatuan"]."\" ";
			if($r["SatuanHarga"] == $satuan["NomorSatuan"])
			{
				$html_satuan_harga .= " selected=\"selected\" ";
				$harga = $satuan["PriceList"];
			}
			$html_satuan_harga .= " >".$satuan["NamaSatuan"]." (".number_format($satuan["PriceList"],$_SESSION["setting.money"],',','.').")</option>";
			$html_hidden_harga .= "<input id=\"harga_".$satuan["NomorSatuan"]."\" type=\"hidden\" value=\"".$satuan["PriceList"]."\">";
		}
	}
	$jumlah_unit = 0;
	$html_shade = "";
	$get_shade = mysql_query("SELECT * FROM web_cart_shade WHERE cart_detail_id = ".$r["id"]." AND status = 1");
	$count_shade = mysql_num_rows($get_shade);
	if($count_shade > 0)
	{
		$no = 1;
		while($shade = mysql_fetch_array($get_shade))
		{
			$jumlah_unit += $shade["quantity"];
			$html_shade .= "
			<tr>
			<td>$no</td>
			<td>".$shade["shade"]."</td>
			<td>".number_format($shade["quantity"],$_SESSION["setting.decimal"])."</td>
			<td>
				<a href=\"?m=$menu&md=cart_shade_edit&id=".$shade["id"]."\"><img src=\"../images/user_edit.png\" border=\"0\" /></a>
				<a class=\"ask\" href=\"?m=$menu&md=cart_shade_delete&id=".$shade["id"]."\"><img src=\"../images/trash.png\" border=\"0\" /></a>
			</td>
			</tr>";
			$no++;
		}
	}
	$get_satuan_unit = mysql_query("
	SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, decKonversi AS Konversi,
		CASE
			WHEN intNomor = 0 THEN 0
			WHEN intNomor = intNomorMSatuan1 THEN 1
			WHEN intNomor = intNomorMSatuan2 THEN decKonversi2
			WHEN intNomor = intNomorMSatuan3 THEN decKonversi2 * decKonversi3
		ELSE 0
		END AS KonversiSatuan
	FROM (
		SELECT 1 AS intOrder, b.intNomor , b.vcNama,
			CASE
				WHEN 0 = 0 THEN 0
				WHEN 0 = intNomorMSatuan1 THEN 1
				WHEN 0 = intNomorMSatuan2 THEN 1/decKonversi2
				WHEN 0 = intNomorMSatuan3 THEN 1/(decKonversi2 * decKonversi3)
			ELSE 0
			END AS decKonversi,
		a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
		FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1
		WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
		UNION ALL
		SELECT 2 AS intOrder, b.intNomor , b.vcNama,
			CASE 
				WHEN 0 = 0 THEN 0
				WHEN 0 = intNomorMSatuan1 THEN decKonversi2
				WHEN 0 = intNomorMSatuan2 THEN 1
				WHEN 0 = intNomorMSatuan3 THEN 1/decKonversi3
			ELSE 0
			END AS decKonversi,
		a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
		FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2
		WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi2 > 0
		UNION ALL
		SELECT 3 AS intOrder, b.intNomor , b.vcNama,
			CASE
				WHEN 0 = 0 THEN 0
				WHEN 0 = intNomorMSatuan1 THEN (decKonversi2 * decKonversi3)
				WHEN 0 = intNomorMSatuan2 THEN decKonversi3
				WHEN 0 = intNomorMSatuan3 THEN 1
			ELSE 0
			END AS decKonversi,
		a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
		FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
		WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi3 > 0)
	a ORDER BY a.intOrder");
	$konversi_unit = 0;
	$html_satuan_unit = "<option value=\"\">--pilih satuan unit--</option>";
	$html_hidden_konversi = "";
	while($satuan = mysql_fetch_array($get_satuan_unit))
	{
		if($satuan["KonversiSatuan"] > 0)
		{
			$html_satuan_unit .= "<option value=\"".$satuan["NomorSatuan"]."\" ";
			if($r["SatuanUnit"] == $satuan["NomorSatuan"])
			{
				$html_satuan_unit .= " selected=\"selected\" ";
				$konversi_unit = $satuan["KonversiSatuan"];
			}
			$html_satuan_unit .= " >".$satuan["NamaSatuan"]." (".number_format($satuan["KonversiSatuan"],$_SESSION["setting.decimal"]).")</option>";
			$html_hidden_konversi .= "<input id=\"konversi_".$satuan["NomorSatuan"]."\" type=\"hidden\" value=\"".$satuan["KonversiSatuan"]."\">";
		}
	}
	$get_diskon = mysql_fetch_array(mysql_query("SELECT discount_rate AS DiskonBrand FROM web_discount WHERE customer_id = ".$_SESSION["login.id"]." AND brand_id = ".$r["NomorBrand"]." AND status = 1"));
	if(empty($get_diskon["DiskonBrand"]))
		$get_diskon["DiskonBrand"] = 0;
	$jumlah_harga = $jumlah_unit * $konversi_unit;
	$netto = $harga - ($harga * $get_diskon["DiskonBrand"] / 100);
	$total = $jumlah_harga * $netto;
?>
	<h2>Edit Order</h2>
	<table><tr><td>
		<div class="form">
			<form action="?m=<?php echo $menu; ?>&md=cart_update" class="cmxform" id="form_cartedit" method="post">
				<fieldset>
					<p>
						<label for="kode">Kode</label>
						<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["KodeBarang"]; ?>" />
						<input name="id" size="30" type="hidden" value="<?php echo $r["id"]; ?>" />
					</p>
					<p>
						<label for="nama">Nama</label>
						<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaBarang"]; ?>" />
					</p>
					<p>
						<label for="kategori">Kategori</label>
						<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaKategori"]; ?>" />
					</p>
					<p>
						<label for="brand">Brand</label>
						<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaBrand"]; ?>" />
					</p>
					<p>
						<label for="group">Group</label>
						<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaGroup"]; ?>" />
					</p>
					<p>
						<label for="tipe">Tipe</label>
						<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaTipe"]; ?>" />
					</p>
					<p>
						<label for="grade">Grade</label>
						<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaGrade"]; ?>" />
					<p>
						<label for="surface">Surface</label>
						<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaSurface"]; ?>" />
					</p>
					<p>
						<label for="jumlah_harga"><b>Jumlah</b></label>
						<input id="jumlah_harga" name="" readonly="yes" size="30" type="text" value="<?php echo number_format($jumlah_harga,$_SESSION["setting.decimal"]); ?>" />
					</p>
					<p>
						<label for="satuan_harga"><font color="#FF0000"><b>Satuan Harga</b></font></label>
						<select id="satuan_harga" name="satuan_harga" style="width: 15em;">
							<?php echo $html_satuan_harga.$html_hidden_harga; ?>
						</select>
					</p>
					<p>
						<label for="jumlah_unit">Jumlah Unit</label>
						<input id="jumlah_unit" name="" readonly="yes" size="30" type="text" value="<?php echo number_format($jumlah_unit,$_SESSION["setting.decimal"]); ?>" />
					</p>
					<p>
						<label for="satuan_unit"><font color="#FF0000"><b>Satuan Unit</b></font></label>
						<select id="satuan_unit" name="satuan_unit" style="width: 15em;">
							<?php echo $html_satuan_unit.$html_hidden_konversi; ?>
						</select>
					</p>
					<p>
						<label for="harga"><b>Harga</b></label>
						<input id="harga" name="" readonly="yes" size="30" style="text-align:right;" type="text" value="<?php echo number_format($harga,$_SESSION["setting.money"]); ?>" />
					</p>
					<p>
						<label for="diskon">Diskon (%)</label>
						<input id="diskon" name="" readonly="yes" size="30" style="text-align:right;" type="text" value="<?php echo $get_diskon["DiskonBrand"]; ?>" />
					</p>
					<p>
						<label for="netto"><b>Netto</b></label>
						<input id="netto" name="" readonly="yes" size="30" style="text-align:right;" type="text" value="<?php echo number_format($netto,$_SESSION["setting.money"]); ?>" />
					</p>
					<p>
						<label for="total"><b>Total</b></label>
						<input id="total" name="" readonly="yes" size="30" style="text-align:right;" type="text" value="<?php echo number_format($total,$_SESSION["setting.money"]); ?>" />
					</p>
					<p><input class="submit" id="simpan" name="simpan" type="submit" value="Simpan ke Cart" /></p>
				</fieldset>
			</form>
		</div>
	</td></tr></table>
	<br />
	<h2>Color Shade <a href="?m=<?php echo $menu; ?>&md=cart_shade_add&id=<?php echo $r["id"]; ?>&id2=<?php echo $r["NomorBarang"]; ?>"><img src="../images/plus.gif"></a></h2>
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<th scope="col" class="rounded">Shade</th>
				<th scope="col" class="rounded">Jumlah</th>
				<th scope="col" class="rounded-q4">Action</th>
			</tr>
		</thead>
		<tbody>
			<?php echo $html_shade; ?>
		</tbody>
	</table>
<?php
	break;
	
	case "cart_update":
	if(!empty($_POST["id"]))
	{
		$update = mysql_query("UPDATE web_cart_detail SET unit1_id = ".$_POST["satuan_harga"].", unit2_id = ".$_POST["satuan_unit"].", date_modified = now() WHERE id = ".$_POST["id"]);
		if($update)
		{
			mysql_query($modif_cart);
			$_SESSION["$menu.warning_box"] = "valid";
			$_SESSION["$menu.warning_msg"] = "Detail barang di dalam cart telah diupdate";
		}
		else
		{
			$_SESSION["$menu.warning_box"] = "error";
			$_SESSION["$menu.warning_msg"] = "Detail barang di dalam cart gagal diupdate";
		}
	}
	die("<meta content='0;URL=?m=$menu&md=cart_edit&id=".$_POST["id"]."' http-equiv='refresh' />");
	break;
	
	case "cart_delete":
	if(!empty($_GET["id"]))
	{
		$delete = mysql_query("UPDATE web_cart_detail SET status = 0, date_deleted = now() WHERE id = ".$_GET["id"]);
		if($delete)
		{
			$_SESSION["cart.warning_box"] = "valid";
			$_SESSION["cart.warning_msg"] = "Barang telah dihapus dari cart";
			$count_detail = mysql_num_rows(mysql_query("SELECT * FROM web_cart_detail WHERE cart_id = ".$_SESSION["cart.id"]." AND status = 1"));
			if($count_detail < 1)
			{
				$delete = mysql_query("UPDATE web_cart SET status = 2, date_deleted = now() WHERE id = ".$_SESSION["cart.id"]);
				unset($_SESSION["cart.id"]);
				unset($_SESSION["cart.warning_box"]);
				unset($_SESSION["cart.warning_msg"]);
				$_SESSION["barang.warning_box"] = "valid";
				$_SESSION["barang.warning_msg"] = "Cart telah dibatalkan";
				die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
			}
		}
		else
		{
			$_SESSION["cart.warning_box"] = "error";
			$_SESSION["cart.warning_msg"] = "Barang gagal dihapus dari cart";
		}
	}
	die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
	break;
	
	case "cart_discard":
	if(!empty($_GET["id"]))
	{
		$discard = mysql_query("UPDATE web_cart SET status = 2, date_deleted = now() WHERE id = ".$_GET["id"]);
		if($discard)
		{
			unset($_SESSION["cart.id"]);
			$_SESSION["barang.warning_box"] = "valid";
			$_SESSION["barang.warning_msg"] = "Cart telah dibatalkan";
			die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
		}
		else
		{
			$_SESSION["cart.warning_box"] = "error";
			$_SESSION["cart.warning_msg"] = "Cart gagal dibatalkan";
			die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
		}
	}
	break;
	
	case "cart_post":
	if(!empty($_SESSION["cart.id"]))
	{
		$msg1 ="Posting Order belum berhasil";
		$msg2 ="<br /> Silahkan mencoba kembali setelah beberapa saat. 
				<br /> Jika Anda masih menemui pesan error ini, segera hubungi <b>Admnistrator Website</b>.";
		$msg3 ="<br />Segera hubungi <b>Administrator Website</b> untuk memastikan tidak terjadi redudansi data order.";
		$turn_off = mysql_query("SET AUTOCOMMIT = OFF");
		if(!$turn_off)
		{
			$_SESSION["cart.warning_box"] = "warning";
			$_SESSION["cart.warning_msg"] = "$msg1 - setting <b>autocommit</b> bermasalah. $msg2";
			die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
		}
		$start = mysql_query("START TRANSACTION");
		if(!$start)
		{
			$_SESSION["cart.warning_box"] = "warning";
			$_SESSION["cart.warning_msg"] = "$msg1 - setting <b>transactions<b> bermasalah. $msg2";
			die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
		}
		$cart_id = $_SESSION["cart.id"];
		$valuta = $_SESSION["setting.valuta"];
		$get_detail = mysql_query("
		SELECT d.id, d.good_id AS NomorBarang, b.vcNamaBeli AS NamaBarang, 
		d.unit1_id AS NomorSatuanHarga, sh.vcNama AS NamaSatuanHarga, d.unit2_id AS NomorSatuanUnit, su.vcNama AS NamaSatuanUnit
		FROM web_cart_detail d 
		JOIN mbarang b ON d.good_id = b.intNomor
		JOIN msatuan sh ON d.unit1_id = sh.intNomor
		JOIN msatuan su ON d.unit2_id = su.intNomor
		WHERE d.cart_id = $cart_id AND d.status = 1 AND b.intStatus = 1 AND b.intAktif = 1");
		$i = 1;
		$total_akhir = 0;
		while($r = mysql_fetch_array($get_detail))
		{
			$jumlah_unit = 0;
			$get_shade = mysql_query("SELECT * FROM web_cart_shade WHERE cart_detail_id = ".$r["id"]." AND status = 1");
			$count_shade = mysql_num_rows($get_shade);
			if($count_shade > 0)
			{
				$detail_id = $r["id"];
				$j = 1;
				while($s = mysql_fetch_array($get_shade))
				{
					$arr_shade["$detail_id"] = array(
					//	"sequence" => $j,
						"name" => $s["shade"],
						"quantity" => $s["quantity"]
					);
					$jumlah_unit += $s["quantity"];
					$j++;
				}
			}
			else
			{
				$_SESSION["cart.warning_box"] = "warning";
				$_SESSION["cart.warning_msg"] = "$msg1 - shade dari barang <u>".$r["NamaBarang"]."</u> belum ditentukan";
				die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
			}
			if($jumlah_unit <= 0)
			{
				$_SESSION["cart.warning_box"] = "warning";
				$_SESSION["cart.warning_msg"] = "$msg1 - jumlah shade dari barang ".$r["NamaBarang"]." tidak valid";
				die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
			}
			$nomor_barang = $r["NomorBarang"];
			$get_satuan_harga = mysql_fetch_array(mysql_query("
			SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, intNomorMSatuan1 AS NomorSatuan1, decKonversi AS NilaiKonversi, decHarga AS PriceList
			FROM (
				SELECT 1 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, 1 AS decKonversi,
					CASE
						WHEN $valuta = 1 THEN a.decHargaPL1
						WHEN $valuta = 2 THEN a.decHargaPL1USD
						WHEN $valuta = 3 THEN a.decHargaPL1RMB
					END AS decHarga
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
				UNION ALL
				SELECT 2 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 AS decKonversi,
					CASE
						WHEN $valuta = 1 THEN a.decHargaPL2
						WHEN $valuta = 2 THEN a.decHargaPL2USD
						WHEN $valuta = 3 THEN a.decHargaPL2RMB
					END AS decHarga
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
				UNION ALL
				SELECT 3 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 * decKonversi3 AS decKonversi,
					CASE 
						WHEN $valuta = 1 THEN a.decHargaPL3
						WHEN $valuta = 2 THEN a.decHargaPL3USD
						WHEN $valuta = 3 THEN a.decHargaPL3RMB
					END AS decHarga
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
				WHERE a.intNomor = $nomor_barang AND b.intNomor > 0)
			a WHERE a.intNomor = ".$r["NomorSatuanHarga"]." ORDER BY a.intOrder"));
			$harga = $get_satuan_harga["PriceList"];
			if(empty($harga) || $harga <= 0)
			{
				$_SESSION["cart.warning_box"] = "warning";
				$_SESSION["cart.warning_msg"] = "$msg1 - satuan dari barang ".$r["NamaBarang"]." tidak valid";
				die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
			}
			$get_satuan_unit = mysql_fetch_array(mysql_query("
			SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, decKonversi AS Konversi,
				CASE
					WHEN intNomor = 0 THEN 0
					WHEN intNomor = intNomorMSatuan1 THEN 1
					WHEN intNomor = intNomorMSatuan2 THEN decKonversi2
					WHEN intNomor = intNomorMSatuan3 THEN decKonversi2 * decKonversi3
				ELSE 0
				END AS KonversiSatuan
			FROM (
				SELECT 1 AS intOrder, b.intNomor , b.vcNama,
					CASE
						WHEN 0 = 0 THEN 0
						WHEN 0 = intNomorMSatuan1 THEN 1
						WHEN 0 = intNomorMSatuan2 THEN 1/decKonversi2
						WHEN 0 = intNomorMSatuan3 THEN 1/(decKonversi2 * decKonversi3)
					ELSE 0
					END AS decKonversi,
				a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1
				WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
				UNION ALL
				SELECT 2 AS intOrder, b.intNomor , b.vcNama,
					CASE 
						WHEN 0 = 0 THEN 0
						WHEN 0 = intNomorMSatuan1 THEN decKonversi2
						WHEN 0 = intNomorMSatuan2 THEN 1
						WHEN 0 = intNomorMSatuan3 THEN 1/decKonversi3
					ELSE 0
					END AS decKonversi,
				a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2
				WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi2 > 0
				UNION ALL
				SELECT 3 AS intOrder, b.intNomor , b.vcNama,
					CASE
						WHEN 0 = 0 THEN 0
						WHEN 0 = intNomorMSatuan1 THEN (decKonversi2 * decKonversi3)
						WHEN 0 = intNomorMSatuan2 THEN decKonversi3
						WHEN 0 = intNomorMSatuan3 THEN 1
					ELSE 0
					END AS decKonversi,
				a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
				FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
				WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi3 > 0)
			a WHERE a.intNomor = ".$r["NomorSatuanUnit"]." ORDER BY a.intOrder"));
			$konversi_unit = $get_satuan_unit["KonversiSatuan"];
			if(empty($konversi_unit) || $konversi_unit <= 0)
			{
				$_SESSION["cart.warning_box"] = "warning";
				$_SESSION["cart.warning_msg"] = "$msg1 - satuan unit dari barang ".$r["NamaBarang"]." tidak valid";
				die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
			}
			$get_diskon = mysql_fetch_array(mysql_query("SELECT discount_rate AS DiskonBrand FROM web_discount WHERE customer_id = ".$_SESSION["login.id"]." AND brand_id = ".$r["NomorBrand"]." AND status = 1"));
			$diskon = $get_diskon["DiskonBrand"];
			if(empty($diskon))
				$diskon = 0;
			$jumlah_harga = $jumlah_unit * $konversi_unit;
			$netto = $harga - ($harga * $diskon / 100);
			$total = $jumlah_harga * $netto;
			$total_akhir += $total;
			$detail_id["$i"] = $r["id"];
			$arr_detail["$i"] = array(	
				"id" => $r["id"],
				"urutan" => $i,
				"nomor_barang" => $nomor_barang,
				"nama_barang" => $r["NamaBarang"],
				"nomor_satuan_harga" => $r["NomorSatuanHarga"],
				"nomor_satuan_harga1" => $get_satuan_harga["NomorSatuan1"],
				"nomor_satuan_unit" => $r["NomorSatuanUnit"],
				"konversi_satuan_harga" => $get_satuan_harga["NilaiKonversi"],
				"konversi_satuan_unit" => $get_satuan_unit["Konversi"],
				"jumlah_harga" => $jumlah_harga,
				"jumlah_unit" => $jumlah_unit,
				"harga" => $harga,
				"diskon" => $diskon,
				"netto" => $netto,
				"total" => $total
			);
			$i++;
		}
		include "../php/config_mysqli.php";
		$result = $mysqli->query("call SP_GenerateCode_20110308 ('Order Jual', '', 'SO/', '%y%m/', 5, '2012-12-20')");
		while($row = $result->fetch_assoc())
			$kode["vcKodeBaru"] = $row["vcKodeBaru"];
		if(empty($kode["vcKodeBaru"]) || $kode["vcKodeBaru"] == '')
		{
			$_SESSION["cart.warning_box"] = "error";
			$_SESSION["cart.warning_msg"] = "$msg1 - kode Order gagal digenerate. $msg2";
			die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
		}
		$header = mysql_query("
		INSERT INTO THOrderJual (
		vcKode,
		intTOP,
		dtTanggal,
		dtTanggalKirim,
		intJTHari,
		vcKeterangan,
		vcKeteranganFJ,
		vcKeteranganKW,
		intJenis,
		intNomorMCabang,
		intNomorMCustomer,
		intNomorMSales,
		intNomorMJenisPenjualan,
		intNomorMValuta,
		decSubTotal,
		decKurs,
		decTotal,
		decDPP,
		decSisa,
		intInsertUserID,
		dtInsertTime,
		vcPenerima,
		vcAlamat,
		vcTelepon,
		vcFax,
		vcHP,
		vcEmail,
		intNomorMGudang,
		intNomorMArea,
		vcAlasanValidasi,
		vcAlasanBatalValidasi)
		VALUES (
		'".$kode["vcKodeBaru"]."',
		3,
		now(),
		'".date("Y-m-d")."',
		30,
		'".$_POST["keterangan"]."',
		'',
		'',
		1,
		10,
		".$_SESSION["login.id"].",
		1,
		11,
		1,
		$total_akhir,
		1,
		$total_akhir,
		$total_akhir,
		$total_akhir,
		5,
		now(),
		'',
		'',
		'',
		'',
		'',
		'',
		22,
		1,
		'',
		'')");
		if(!$header)
		{
			$_SESSION["cart.warning_box"] = "error";
			$_SESSION["cart.warning_msg"] = "$msg1 - data header gagal disimpan. $msg2";
			die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
		}
		$intNomorHeader = mysql_insert_id();
		for($x=1;$x<$i;$x++)
		{
			$detail = mysql_query("
			INSERT INTO TDOrderJual (
			intNomorHeader,
			intNomorDetail,
			intNomorMBarang,
			intNomorMSatuan,
			intNomorMSatuan1,
			intNomorMSatuanUnit,
			decMultiplier1,
			decMultiplier,
			decJumlah1,
			decJumlahUnit,
			decJumlah,
			decHarga,
			decDisc1,
			decNetto,
			decSubTotal,
			intInsertUserID,
			dtInsertTime)
			VALUES (
			$intNomorHeader,
			".$arr_detail["$x"]["urutan"].",
			".$arr_detail["$x"]["nomor_barang"].",
			".$arr_detail["$x"]["nomor_satuan_harga"].",
			".$arr_detail["$x"]["nomor_satuan_harga1"].",
			".$arr_detail["$x"]["nomor_satuan_unit"].",
			".$arr_detail["$x"]["konversi_satuan_harga"].",
			".$arr_detail["$x"]["konversi_satuan_unit"].",
			".$arr_detail["$x"]["jumlah_harga"].",
			".$arr_detail["$x"]["jumlah_unit"].",
			".$arr_detail["$x"]["jumlah_harga"].",
			".$arr_detail["$x"]["harga"].",
			".$arr_detail["$x"]["diskon"].",
			".$arr_detail["$x"]["netto"].",
			".$arr_detail["$x"]["total"].",
			5,
			now())");
			if(!$detail)
			{
				$_SESSION["cart.warning_box"] = "error";
				$_SESSION["cart.warning_msg"] = "$msg1 - data detail barang ".$arr_detail["$x"]["nama_barang"]." gagal disimpan. $msg2";
				$rollback = mysql_query("ROLLBACK");
				if(!$rollback)
					$_SESSION["cart.warning_msg"] .= "<br />Rollback transaction gagal. $msg3";
				die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
			}
			$detail_id = $arr_detail["$x"]["id"];
			for($y=1;$y<$j;$y++)
			{
				$shade = mysql_query("
				INSERT INTO TDOrderJualShade (
				intNomorHeader,
				intNomorDetail,
				intNomorMBarang,
				vcShade,
				decJumlah,
				intInsertUserID,
				dtInsertTime)
				VALUES (
				$intNomorHeader,
				$y,
				".$arr_detail["$x"]["nomor_barang"].",
				'".$arr_shade["$detail_id"]["name"]."',
				".$arr_shade["$detail_id"]["quantity"].",
				5,
				now())");
				if(!$shade)
				{
					$_SESSION["cart.warning_box"] = "error";
					$_SESSION["cart.warning_msg"] = "$msg1 - data shade ".$arr_shade["$detail_id"]["name"]." dari barang ".$arr_detail["$x"]["nama_barang"]." gagal disimpan. $msg2";
					$rollback = mysql_query("ROLLBACK");
					if(!$rollback)
						$_SESSION["cart.warning_msg"] .= "<br />Rollback transaction gagal. $msg3";
					die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
				}
			}
		}
		$commit = mysql_query("COMMIT");
		if(!$commit)
		{
			$_SESSION["cart.warning_box"] = "error";
			$_SESSION["cart.warning_msg"] = "$msg1 - commit transaction gagal. $msg2";
			die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
		}
		unset($_SESSION["cart.id"]);
		$delete_cart = mysql_query("UPDATE web_cart SET order_id = $intNomorHeader, date_deleted = now(), status = 2 WHERE id = $cart_id");
		if(!$delete_cart)
		{
			$_SESSION["cart.warning_box"] = "warning";
			$_SESSION["cart.warning_msg"] .= "Cart berhasil diorder namun status cart gagal diperbaharui. $msg3";
			die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
		}
		$commit = mysql_query("COMMIT");
		if(!$commit)
		{
			$_SESSION["cart.warning_box"] = "warning";
			$_SESSION["cart.warning_msg"] = "Cart berhasil diorder namun status cart gagal diperbaharui. $msg3";
			die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
		}
		$_SESSION["order.warning_box"] = "valid";
		$_SESSION["order.warning_msg"] .= "Cart berhasil diorder";
		die("<meta content='0;URL=?m=$menu&md=order_status&id=".$cart_id."' http-equiv='refresh' />");
	}
	die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
	break;
	
	case "cart_shade_add":
	$get_shade = mysql_query("SELECT DISTINCT vcShade FROM TLaporanStok WHERE intNomorMBarang = ".$_GET["id2"]." ORDER BY vcShade");
?>
	<h2>Tambah Color Shade</h2>
	<table><tr><td>
		<div class="form">
			<form action="?m=<?php echo $menu; ?>&md=cart_shade_insert&id=<?php echo $_GET["id"]; ?>" id="form_shade" class="niceform" method="post">
				<fieldset>
					<dl>
						<dt><label for="shade">Shade&nbsp;:&nbsp;</label></dt>
						<dd><select size="1" name="shade" id="shade">
							<option value="">--pilih shade--</option>
							<?php
							while($r = mysql_fetch_array($get_shade))
								echo "<option value=\"".$r["vcShade"]."\">".$r["vcShade"]."</option>";
							?>
							</select>
						</dd>
					</dl>
					<dl>
						<dt><label for="jumlah">Jumlah&nbsp;:&nbsp;</label></dt>
						<dd>
							<input id="jumlah" name="jumlah" size="30" type="text" value="" />
						</dd>
					</dl>
				<dl class="submit"><input id="submit" name="submit" type="submit" value="Tambah ke Cart" /></dl>
				</fieldset>
			</form>
		</div>
	</td></tr></table>
<?php
	break;
	
	case "cart_shade_insert":
	if(!empty($_GET["id"]) && !empty($_POST["shade"]) && !empty($_POST["jumlah"]))
	{
		$get_shade = mysql_query("SELECT * FROM web_cart_shade WHERE cart_detail_id = ".$_GET["id"]." AND shade = '".$_POST["shade"]."' AND status = 1");
		$count_shade = mysql_num_rows($get_shade);
		if($count_shade > 0)
		{
			$shade = mysql_fetch_array($get_shade);
			$new_quantity = $shade["quantity"] + $_POST["jumlah"];
			$update = mysql_query("
			UPDATE web_cart_shade SET shade = '".$_POST["shade"]."', quantity = $new_quantity, date_modified = now()
			WHERE id = ".$shade["id"]);
			if($update)
			{
				mysql_query($modif_cart);
				$_SESSION["$menu.warning_box"] = "warning";
				$_SESSION["$menu.warning_msg"] = "Shade ditambahkan ke order yang sebelumnya";
			}
			else
			{
				$_SESSION["$menu.warning_box"] = "error";
				$_SESSION["$menu.warning_msg"] = "Shade gagal ditambahkan";	
			}
		}
		else
		{
			$insert = mysql_query("
			INSERT INTO web_cart_shade (cart_detail_id,shade,quantity,date_created)
			VALUES (".$_GET["id"].",'".$_POST["shade"]."',".$_POST["jumlah"].",now())");
			if($insert)
			{
				mysql_query($modif_cart);
				$_SESSION["$menu.warning_box"] = "valid";
				$_SESSION["$menu.warning_msg"] = "Shade telah ditambahkan";
			}
			else
			{
				$_SESSION["$menu.warning_box"] = "error";
				$_SESSION["$menu.warning_msg"] = "Shade gagal ditambahkan";	
			}
		}
	}
	die("<meta content='0;URL=?m=$menu&md=cart_edit&id=".$_GET["id"]."' http-equiv='refresh' />");
	break;
	
	case "cart_shade_edit":
	$r = mysql_fetch_array(mysql_query("
	SELECT s.id, s.cart_detail_id, s.shade, s.quantity, d.good_id
	FROM web_cart_shade s 
	JOIN web_cart_detail d ON s.cart_detail_id = d.id
	WHERE s.id = ".$_GET["id"]));
	$get_shade = mysql_query("SELECT DISTINCT vcShade FROM TLaporanStok WHERE intNomorMBarang = ".$r["good_id"]." ORDER BY vcShade");
?>
	<h2>Edit Color Shade</h2>
	<table><tr><td>
		<div class="form">
			<form action="?m=<?php echo $menu; ?>&md=cart_shade_update&id=<?php echo $r["id"]; ?>" id="form_shade" class="niceform" method="post">
				<fieldset>
					<dl>
						<dt><label for="shade">Shade&nbsp;:&nbsp;</label></dt>
						<dd><select size="1" name="shade" id="shade">
							<option value="">--pilih shade--</option>
							<?php
							while($s = mysql_fetch_array($get_shade))
							{
								echo "<option ";
								if($s["vcShade"] == $r["shade"])
									echo "selected=\"selected\"";
								echo " value=\"".$s["vcShade"]."\">".$s["vcShade"]."</option>";
							}
							?>
							</select>
						</dd>
					</dl>
					<dl>
						<dt><label for="jumlah">Jumlah&nbsp;:&nbsp;</label></dt>
						<dd>
							<input id="jumlah" name="jumlah" size="30" type="text" value="<?php echo number_format($r["quantity"],$_SESSION["setting.decimal"]); ?>" />
						</dd>
					</dl>
				<dl class="submit"><input id="submit" name="submit" type="submit" value="Simpan ke Cart" /></dl>
				</fieldset>
			</form>
		</div>
	</td></tr></table>
<?php
	break;
	
	case "cart_shade_update":
	$get_shade = mysql_fetch_array(mysql_query("SELECT * FROM web_cart_shade WHERE id = ".$_GET["id"]));
	if(!empty($_GET["id"]) && !empty($_POST["shade"]) && !empty($_POST["jumlah"]))
	{
		$update = mysql_query("
		UPDATE web_cart_shade SET shade = '".$_POST["shade"]."', quantity = ".$_POST["jumlah"].", date_modified = now()
		WHERE id = ".$_GET["id"]);
		if($update)
		{
			mysql_query($modif_cart);
			$_SESSION["$menu.warning_box"] = "valid";
			$_SESSION["$menu.warning_msg"] = "Perubahan shade telah disimpan";
		}
		else
		{
			$_SESSION["$menu.warning_box"] = "error";
			$_SESSION["$menu.warning_msg"] = "Perubahan shade gagal disimpan";	
		}
	}
	die("<meta content='0;URL=?m=$menu&md=cart_edit&id=".$get_shade["cart_detail_id"]."' http-equiv='refresh' />");
	break;
	
	case "cart_shade_delete":
	$get_shade = mysql_fetch_array(mysql_query("SELECT * FROM web_cart_shade WHERE id = ".$_GET["id"]));
	if(!empty($_GET["id"]))
	{
		$delete = mysql_query("UPDATE web_cart_shade SET status = 0 WHERE id = ".$_GET["id"]);
		if($delete)
		{
			mysql_query($modif_cart);
			$_SESSION["$menu.warning_box"] = "valid";
			$_SESSION["$menu.warning_msg"] = "Shade telah dihapus";
		}
		else
		{
			$_SESSION["$menu.warning_box"] = "error";
			$_SESSION["$menu.warning_msg"] = "Shade gagal dihapus";	
		}
	}
	die("<meta content='0;URL=?m=$menu&md=cart_edit&id=".$get_shade["cart_detail_id"]."' http-equiv='refresh' />");
	break;
	
	case "draft":
?>
	<h2>Draft Order</h2>
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<th scope="col" class="rounded">Kode</th>
				<th scope="col" class="rounded">Tanggal</th>
				<th scope="col" class="rounded-q4">Action</th>
			</tr>
		</thead>
		<tbody>
<?php
	$page = new Paging;
	$batas = $_SESSION["setting.row"];
	$posisi = $page->cariPosisi($batas);
	$get_draft = mysql_query("SELECT * FROM web_cart WHERE customer_id = ".$_SESSION["login.id"]." AND status = 2 AND order_id IS NULL ORDER BY date_deleted DESC LIMIT $posisi, $batas");
	$baris = mysql_num_rows(mysql_query("SELECT * FROM web_cart WHERE status = 2 AND order_id IS NULL"));
	$halaman = $page->jumlahHalaman($baris,$batas);
	$link = $page->navHalaman($_GET["p"],$halaman);
	$no = $posisi+1;
	while($r = mysql_fetch_array($get_draft))
	{
		$dt = explode(" ",$r["date_created"]);
		$d = explode("-",$dt[0]);
		echo"
		<tr>
		<td>$no</td>
		<td><a href=\"?m=$menu&md=draft_detail&id=".$r["id"]."\">Cart #".$r["id"]."</a></td>
		<td>$d[2]-$d[1]-$d[0] $dt[1]</td>
		<td>
			<a class=\"ask\" href=\"?m=$menu&md=draft_activate&id=".$r["id"]."\"><img src=\"../images/valid.gif\"></a>
			<a class=\"ask\" href=\"?m=$menu&md=draft_delete&id=".$r["id"]."\"><img src=\"../images/trash.png\"></a>
		</td>
		</tr>";
		$no++;
	}
?>
		</tbody>
	</table>
<?php
	include "../php/pagination.php";
	break;
	
	case "draft_detail":
	$get_draft = mysql_fetch_array(mysql_query("SELECT * FROM web_cart WHERE id = ".$_GET["id"]));
	$dt = explode(" ",$get_draft["date_created"]);
	$d = explode("-",$dt[0]);
?>
	<h2>Draft Cart #<?php echo $get_draft["id"]; ?></h2>
	<i><?php echo "$d[2]-$d[1]-$d[0] $dt[1]"; ?></i><br /><br />
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<th scope="col" class="rounded">Kode</th>
				<th scope="col" class="rounded">Nama</th>
				<th scope="col" class="rounded">Jumlah</th>
				<th scope="col" class="rounded-q4"></th>
			</tr>
		</thead>
		<tbody>
<?php
	$get_detail = mysql_query("
	SELECT d.quantity, b.vcKode, b.vcNamaBeli
	FROM web_cart_detail d 
	JOIN mbarang b ON d.good_id = b.intNomor
	WHERE d.cart_id = ".$get_draft["id"]." AND d.status = 1 AND b.intStatus = 1 AND b.intAktif = 1");
	$no = 1;
	while($r = mysql_fetch_array($get_detail))
	{
		echo"
		<tr>
		<td>$no</td>
		<td>".$r["vcKode"]."</td>
		<td>".$r["vcNamaBeli"]."</td>
		<td>".number_format($r["quantity"],0)."</td>
		<td></td>
		</tr>";
		$no++;
	}
?>		</tbody>
	</table>
	<a href="#" class="bt_blue" onclick="link_confirmation('Apakah Anda yakin mengaktifkan kembali cart ini?','?m=<?php echo $menu; ?>&md=draft_activate&id=<?php echo $get_draft["id"]; ?>')">
		<span class="bt_blue_lft"></span>
		<strong>Activate</strong>
		<span class="bt_blue_r"></span>
	</a>
	<a href="#" class="bt_red" onclick="link_confirmation('Apakah Anda yakin menghapus cart ini?','?m=<?php echo $menu; ?>&md=draft_delete&id=<?php echo $get_draft["id"]; ?>')">
		<span class="bt_red_lft"></span>
		<strong>Delete</strong>
		<span class="bt_red_r"></span>
	</a>
<?php
	break;
	
	case "draft_activate":
	if(!empty($_SESSION["cart.id"]))
	{
		$_SESSION["$menu.warning_box"] = "warning";
		$_SESSION["$menu.warning_msg"] = "Cart yang sedang aktif harus dibatalkan terlebih dahulu untuk bisa mengaktifkan cart lain";
		die("<meta content='0;URL=?m=$menu&md=draft' http-equiv='refresh' />");
	}
	elseif(!empty($_GET["id"]))
	{
		$conflict_handling = mysql_query("UPDATE web_cart SET status = 2 WHERE customer_id = ".$_SESSION["login.id"]." AND status <> 0");
		if($conflict_handling)
			$update = mysql_query("UPDATE web_cart SET status = 1 WHERE id = ".$_GET["id"]);
		if($update)
		{
			$_SESSION["cart.id"] = $_GET["id"];
			$_SESSION["cart.warning_box"] = "valid";
			$_SESSION["cart.warning_msg"] = "Cart telah diaktifkan kembali";
			die("<meta content='0;URL=?m=barang#cart_pos' http-equiv='refresh' />");
		}
		else
		{
			$_SESSION["$menu.warning_box"] = "error";
			$_SESSION["$menu.warning_msg"] = "Cart gagal diaktifkan kembali";
		}
	}
	die("<meta content='0;URL=?m=$menu&md=draft' http-equiv='refresh' />");	
	break;
	
	case "draft_delete":
	if(!empty($_GET["id"]))
	{
		$delete = mysql_query("UPDATE web_cart SET status = 0, date_deleted = now() WHERE id = ".$_GET["id"]);
		if($delete)
		{
			$_SESSION["$menu.warning_box"] = "valid";
			$_SESSION["$menu.warning_msg"] = "Cart telah dihapus";
		}
		else
		{
			$_SESSION["$menu.warning_box"] = "error";
			$_SESSION["$menu.warning_msg"] = "Cart gagal dihapus";
		}
	}
	die("<meta content='0;URL=?m=$menu&md=draft' http-equiv='refresh' />");
	break;
	
	case "order":
?>
	<h2>Order</h2>
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<th scope="col" class="rounded">Kode Order</th>
				<th scope="col" class="rounded">Cart</th>
				<th scope="col" class="rounded">Tanggal</th>
				<th scope="col" class="rounded-q4">Action</th>
			</tr>
		</thead>
		<tbody>
<?php
	$page = new Paging;
	$batas = $_SESSION["setting.row"];
	$posisi = $page->cariPosisi($batas);
	$get_order = mysql_query("
	SELECT a.id, b.vcKode, b.dtTanggal
	FROM web_cart a
	JOIN thorderjual b ON a.order_id = b.intNomor
	WHERE a.customer_id = ".$_SESSION["login.id"]." AND a.status = 2 AND a.order_id IS NOT NULL 
	ORDER BY a.date_deleted DESC LIMIT $posisi, $batas");
	$baris = mysql_num_rows(mysql_query("
	SELECT a.id, a.date_deleted, b.vcKode
	FROM web_cart a
	JOIN thorderjual b ON a.cart_id = b.intNomor
	WHERE a.customer_id = ".$_SESSION["login.id"]." AND a.status = 2 AND a.order_id IS NOT NULL"));
	$halaman = $page->jumlahHalaman($baris,$batas);
	$link = $page->navHalaman($_GET["p"],$halaman);
	$no = $posisi+1;
	while($r = mysql_fetch_array($get_order))
	{
		$dt = explode(" ",$r["dtTanggal"]);
		$d = explode("-",$dt[0]);
		echo"
		<tr>
		<td>$no</td>
		<td><a href=\"?m=$menu&md=order_status&id=".$r["id"]."\">".$r["vcKode"]."</a></td>
		<td>Cart #".$r["id"]."</td>
		<td>$d[2]-$d[1]-$d[0] $dt[1]</td>
		<td>
			<a class=\"ask\" href=\"?m=$menu&md=order_duplicate&id=".$r["id"]."\"><img src=\"../images/cart.png\" width=\"20\"></a>
		</td>
		</tr>";
		$no++;
	}
?>		</tbody>
	</table>
<?php
	include "../php/pagination.php";
	break;
	
	case "order_status":
	$cart_id = $_GET["id"];
	$get_order = mysql_fetch_array(mysql_query("
	SELECT a.id, b.vcKode, b.dtTanggal, b.intJTHari, b.intStatus, b.intApproved, b.vcKeterangan
	FROM web_cart a
	JOIN thorderjual b ON a.order_id = b.intNomor 
	WHERE a.id = $cart_id"));
	$dt = explode(" ",$get_order["dtTanggal"]);
	$d = explode("-",$dt[0]);
	$deadline = mysql_fetch_array(mysql_query("SELECT DATE_ADD('$dt[0]',INTERVAL ".$get_order["intJTHari"]." DAY) AS JatuhTempo"));
	$jt = explode("-",$deadline["JatuhTempo"]);
?>
	<h2><?php echo $get_order["vcKode"]." (Cart #".$get_order["id"].")"; ?></h2>
	<table>
		<tr><td><b>Tanggal</b></td><td> : <?php echo "$d[2]-$d[1]-$d[0] $dt[1]"; ?></td></tr>
		<tr><td><b>Jatuh tempo</b></td><td> : <?php echo "$jt[2]-$jt[1]-$jt[0]"; ?></td></tr>
		<tr><td><b>Status</b></td><td> : <?php if($get_order["intStatus"] != 0) echo "Aktif"; else echo "Batal"; ?></td></tr>
		<tr><td><b>Approval</b></td><td> : <?php if($get_order["intApproved"] == 1) echo "Approved"; else echo "-"; ?></td></tr>
	</table>
	<p align="right"><a href="preview.php?mdl=print&id=<?php echo $get_order["id"]; ?>"><img src="../images/print.png" width="20"></a></p>
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<th colspan="2" scope="col" class="rounded">Nama</th>
				<th scope="col" class="rounded">Jumlah</th>
				<th scope="col" class="rounded">Satuan</th>
				<th scope="col" class="rounded">Jumlah unit</th>
				<th scope="col" class="rounded">Satuan unit</th>
				<th scope="col" class="rounded">Harga</th>
				<th scope="col" class="rounded">Diskon</th>
				<th scope="col" class="rounded">Netto</th>
				<th scope="col" class="rounded">Total</th>
				<th scope="col" class="rounded-q4"></th>
			</tr>
		</thead>
		<tbody>
<?php
	$get_detail = mysql_query("
	SELECT d.id, d.good_id AS NomorBarang, b.vcNamaBeli AS NamaBarang, 
	d.unit1_id AS NomorSatuanHarga, sh.vcNama AS NamaSatuanHarga, d.unit2_id AS NomorSatuanUnit, su.vcNama AS NamaSatuanUnit
	FROM web_cart_detail d 
	JOIN mbarang b ON d.good_id = b.intNomor
	JOIN msatuan sh ON d.unit1_id = sh.intNomor
	JOIN msatuan su ON d.unit2_id = su.intNomor
	WHERE d.cart_id = $cart_id AND d.status = 1 AND b.intStatus = 1 AND b.intAktif = 1");
	$no = 1;
	$total_akhir = 0;
	while($r = mysql_fetch_array($get_detail))
	{
		$jumlah_unit = 0;
		$html_shade = "";
		$get_shade = mysql_query("SELECT * FROM web_cart_shade WHERE cart_detail_id = ".$r["id"]." AND status = 1");
		$count_shade = mysql_num_rows($get_shade);
		if($count_shade > 0)
		{
			while($shade = mysql_fetch_array($get_shade))
			{
				$jumlah_unit += $shade["quantity"];
				$html_shade .= "
				<tr>
				<td></td>
				<td>".$shade["shade"]."</td>
				<td>".number_format($shade["quantity"],$_SESSION["setting.decimal"])."</td>
				<td colspan=\"9\"></td>
				</tr>";
			}
		}
		else
			$posting = false;
		if($jumlah_unit <= 0)
			$posting = false;
		$valuta = $_SESSION["setting.valuta"];
		$nomor_barang = $r["NomorBarang"];
		$get_satuan_harga = mysql_fetch_array(mysql_query("
		SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, intNomorMSatuan1 AS NomorSatuan1, decKonversi AS NilaiKonversi, decHarga AS PriceList
		FROM (
			SELECT 1 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, 1 AS decKonversi,
				CASE
					WHEN $valuta = 1 THEN a.decHargaPL1
					WHEN $valuta = 2 THEN a.decHargaPL1USD
					WHEN $valuta = 3 THEN a.decHargaPL1RMB
				END AS decHarga
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
			UNION ALL
			SELECT 2 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 AS decKonversi,
				CASE
					WHEN $valuta = 1 THEN a.decHargaPL2
					WHEN $valuta = 2 THEN a.decHargaPL2USD
					WHEN $valuta = 3 THEN a.decHargaPL2RMB
				END AS decHarga
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
			UNION ALL
			SELECT 3 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 * decKonversi3 AS decKonversi,
				CASE 
					WHEN $valuta = 1 THEN a.decHargaPL3
					WHEN $valuta = 2 THEN a.decHargaPL3USD
					WHEN $valuta = 3 THEN a.decHargaPL3RMB
				END AS decHarga
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
			WHERE a.intNomor = $nomor_barang AND b.intNomor > 0)
		a WHERE a.intNomor = ".$r["NomorSatuanHarga"]." ORDER BY a.intOrder"));
		$harga = $get_satuan_harga["PriceList"];
		if(empty($harga))
		{
			$harga = 0;
			$posting = false;
		}
		$get_satuan_unit = mysql_fetch_array(mysql_query("
		SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, decKonversi AS Konversi,
			CASE
				WHEN intNomor = 0 THEN 0
				WHEN intNomor = intNomorMSatuan1 THEN 1
				WHEN intNomor = intNomorMSatuan2 THEN decKonversi2
				WHEN intNomor = intNomorMSatuan3 THEN decKonversi2 * decKonversi3
			ELSE 0
			END AS KonversiSatuan
		FROM (
			SELECT 1 AS intOrder, b.intNomor , b.vcNama,
				CASE
					WHEN 0 = 0 THEN 0
					WHEN 0 = intNomorMSatuan1 THEN 1
					WHEN 0 = intNomorMSatuan2 THEN 1/decKonversi2
					WHEN 0 = intNomorMSatuan3 THEN 1/(decKonversi2 * decKonversi3)
				ELSE 0
				END AS decKonversi,
			a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1
			WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
			UNION ALL
			SELECT 2 AS intOrder, b.intNomor , b.vcNama,
				CASE 
					WHEN 0 = 0 THEN 0
					WHEN 0 = intNomorMSatuan1 THEN decKonversi2
					WHEN 0 = intNomorMSatuan2 THEN 1
					WHEN 0 = intNomorMSatuan3 THEN 1/decKonversi3
				ELSE 0
				END AS decKonversi,
			a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2
			WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi2 > 0
			UNION ALL
			SELECT 3 AS intOrder, b.intNomor , b.vcNama,
				CASE
					WHEN 0 = 0 THEN 0
					WHEN 0 = intNomorMSatuan1 THEN (decKonversi2 * decKonversi3)
					WHEN 0 = intNomorMSatuan2 THEN decKonversi3
					WHEN 0 = intNomorMSatuan3 THEN 1
				ELSE 0
				END AS decKonversi,
			a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
			WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi3 > 0)
		a WHERE a.intNomor = ".$r["NomorSatuanUnit"]." ORDER BY a.intOrder"));
		$konversi_unit = $get_satuan_unit["KonversiSatuan"];
		if(empty($konversi_unit))
		{
			$konversi_unit = 0;
			$posting = false;
		}
		$get_diskon = mysql_fetch_array(mysql_query("SELECT discount_rate AS DiskonBrand FROM web_discount WHERE customer_id = ".$_SESSION["login.id"]." AND brand_id = ".$r["NomorBrand"]." AND status = 1"));
		$diskon = $get_diskon["DiskonBrand"];
		if(empty($diskon))
			$diskon = 0;
		$jumlah_harga = $jumlah_unit * $konversi_unit;
		$netto = $harga - ($harga * $diskon / 100);
		$total = $jumlah_harga * $netto;
		$total_akhir += $total;
		echo"
		<tr>
		<td>$no</td>
		<td colspan=\"2\">".$r["NamaBarang"]."</td>
		<td>".number_format($jumlah_harga,$_SESSION["setting.decimal"],",",".")."</td>
		<td>".$r["NamaSatuanHarga"]."</td>
		<td>".number_format($jumlah_unit,$_SESSION["setting.decimal"],",",".")."</td>
		<td>".$r["NamaSatuanUnit"]."</td>
		<td>".number_format($harga,$_SESSION["setting.money"],",",".")."</td>
		<td>$diskon%</td>
		<td>".number_format($netto,$_SESSION["setting.money"],",",".")."</td>
		<td>".number_format($total,$_SESSION["setting.money"],",",".")."</td>
		<td></td>
		</tr>
		$html_shade";
		$no++;
	}
?>		</tbody>
		<tfoot>
			<tr>
				<td align="right" colspan="10" class="rounded-foot-left"><b>TOTAL</b> : </td>
				<td align="right"><?php echo number_format($total_akhir,$_SESSION["setting.money"],",","."); ?></td>
				<td class="rounded-foot-right">&nbsp;</td>
			</tr>
		</tfoot>
	</table>
	Keterangan: <br />
	<?php echo $get_order["vcKeterangan"]; ?>
	<br /><br />
	<a href="#" class="bt_blue" onclick="link_confirmation('Apakah Anda yakin menduplikasi order ini?','?m=<?php echo $menu; ?>&md=order_duplicate&id=<?php echo $get_order["id"]; ?>')">
		<span class="bt_blue_lft"></span>
		<strong>Duplicate</strong>
		<span class="bt_blue_r"></span>
	</a>
<?php
	break;
	
	case "order_duplicate":
	if(!empty($_SESSION["cart.id"]))
	{
		$_SESSION["$menu.warning_box"] = "warning";
		$_SESSION["$menu.warning_msg"] = "Cart yang sedang aktif harus dibatalkan terlebih dahulu untuk bisa menduplikasi cart baru";
		die("<meta content='0;URL=?m=$menu' http-equiv='refresh' />");
	}
	elseif(!empty($_GET["id"]))
	{
		$msg1 ="Duplikasi Order belum berhasil";
		$msg2 ="<br /> Silahkan mencoba kembali setelah beberapa saat. 
				<br /> Jika Anda masih menemui pesan error ini, segera hubungi <b>Admnistrator Website</b>.";
		$msg3 ="<br />Segera hubungi <b>Administrator Website</b> untuk memastikan tidak terjadi redudansi data order.";
		$turn_off = mysql_query("SET AUTOCOMMIT = OFF");
		if(!$turn_off)
		{
			$_SESSION["order.warning_box"] = "warning";
			$_SESSION["order.warning_msg"] = "$msg1 - setting <b>autocommit</b> bermasalah. $msg2";
			die("<meta content='0;URL=?m=$menu' http-equiv='refresh' />");
		}
		$start = mysql_query("START TRANSACTION");
		if(!$start)
		{
			$_SESSION["order.warning_box"] = "warning";
			$_SESSION["order.warning_msg"] = "$msg1 - setting <b>transactions<b> bermasalah. $msg2";
			die("<meta content='0;URL=?m=$menu' http-equiv='refresh' />");
		}
		$insert_header = mysql_query("INSERT INTO web_cart (customer_id,date_created) VALUES (".$_SESSION["login.id"].",now())");
		if(!$insert_header)
		{
			$_SESSION["order.warning_box"] = "error";
			$_SESSION["order.warning_msg"] = "$msg1 - data header gagal disimpan";
			die("<meta content='0;URL=?m=$menu' http-equiv='refresh' />");
		}
		$new_cart_id = mysql_insert_id();
		$get_detail = mysql_query("SELECT * FROM web_cart_detail WHERE cart_id = ".$_GET["id"]." AND status = 1");
		while($r = mysql_fetch_array($get_detail))
		{
			$insert_detail = mysql_query("INSERT INTO web_cart_detail (cart_id,good_id,date_created) VALUES ($new_cart_id,".$r["good_id"].",now())");
			if(!$insert_detail)
			{
				$_SESSION["order.warning_box"] = "error";
				$_SESSION["order.warning_msg"] = "$msg1 - data detail gagal disimpan";
				die("<meta content='0;URL=?m=$menu' http-equiv='refresh' />");
			}
			$new_cart_detail_id = mysql_insert_id();
			$get_shade = mysql_query("SELECT * FROM web_cart_shade WHERE cart_detail_id = ".$r["id"]." AND status = 1");
			while($s = mysql_fetch_array($get_shade))
			{
				$insert_shade = mysql_query("INSERT INTO web_cart_shade (cart_detail_id,shade,quantity,date_created) VALUES ($new_cart_detail_id,'".$s["shade"]."',".$s["quantity"].",now())");
				if(!$insert_shade)
				{
					$_SESSION["order.warning_box"] = "error";
					$_SESSION["order.warning_msg"] = "$msg1 - data shade gagal disimpan";
					die("<meta content='0;URL=?m=$menu' http-equiv='refresh' />");
				}
			}
		}
		$commit = mysql_query("COMMIT");
		if(!$commit)
		{
			$_SESSION["order.warning_box"] = "warning";
			$_SESSION["order.warning_msg"] = "$msg1 - transaksi gagal disimpan. $msg3";
			die("<meta content='0;URL=?m=$menu' http-equiv='refresh' />");
		}
		$_SESSION["cart.id"] = $new_cart_id;
		$_SESSION["cart.warning_box"] = "valid";
		$_SESSION["cart.warning_msg"] = "Cart baru telah berhasil diduplikasi";
	}
	die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
	break;
}
?>